﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LitJson;
using SufeiUtil;

namespace Live
{
    public class QuanMinTv:BasePlatform
    {
        private readonly QuanMinTvRoom room = new QuanMinTvRoom();

        public override bool Init(string url)
        {
            string roomId = GetRoomId(url);

            string html = ReqRoomInfo(roomId);

            JsonData data = JsonMapper.ToObject(html);
            if (data.Keys.Contains("code"))
            {
                //直播间不存在
                return false;
            }

            room.ID = data["uid"].ToString();
            room.Name = data["title"].ToString();
            room.UserName = data["nick"].ToString();
            room.UserAvatar = data["avatar"].ToString();

            return true;
        }

        protected override string ReqRoomInfo(string roomId)
        {
            string url = String.Format("http://m.quanmin.tv/json/rooms/{0}/noinfo6.json", roomId);

            HttpItem item = new HttpItem();
            item.Method = "GET";
            item.URL = url;
            item.Host = "m.quanmin.tv";
            item.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            item.UserAgent =
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";

            return mHelper.GetHtml(item).Html;
        }


        public override bool IsLive()
        {
            throw new NotImplementedException();
        }

        public override Room GetRoomInfo()
        {
            throw new NotImplementedException();
        }

        public override string GetLivePlayUrl()
        {
            return string.Format("http://flv.quanmin.tv/live/{0}.flv",room.ID);
        }
    }
}
